package br.com.brasilprojetos;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import br.com.brasilprojetos.geometric.GameEntity;
import br.com.brasilprojetos.geometric.Geometric;

public class SpaceHeroRenderer implements Renderer {

	private Geometric triangle;
	private Geometric square;
	private Geometric pentagon;
	private Geometric hexagon;

	private float initY = 0;
	private float initX = 0;
	//private float diffX = 0;
	//private float diffY = 0;

	//private float rotation = 0;

	/**
	 * Instance the Triangle and Square objects
	 */
	public SpaceHeroRenderer() {
		
		triangle = new Geometric(150, 150, 3, 50);
		square = new Geometric(330, 250, 4, 50);
		pentagon = new Geometric(150, 450, 9, 50);
		hexagon = new Geometric(300, 600, 10, 50);

		triangle.setFillMode(GL10.GL_LINE_LOOP);
		pentagon.setFillMode(GL10.GL_LINE_LOOP);
	}

	/**
	 * The Surface is created/init()
	 */
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glShadeModel(GL10.GL_SMOOTH); // Enable Smooth Shading
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
		gl.glClearDepthf(1.0f); // Depth Buffer Setup
		gl.glEnable(GL10.GL_DEPTH_TEST); // Enables Depth Testing
		gl.glDepthFunc(GL10.GL_LEQUAL); // The Type Of Depth Testing To Do

		// Really Nice Perspective Calculations
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
	}

	/**
	 * Here we do our drawing
	 */
	public void onDrawFrame(GL10 gl) {
		// Clear Screen And Depth Buffer
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity(); // Reset The Current Modelview Matrix
		
		triangle.draw(gl);
		square.draw(gl);
		pentagon.draw(gl);
		hexagon.draw(gl);

	}

	/**
	 * If the surface changes, reset the view
	 */
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		if (height == 0) { // Prevent A Divide By Zero By
			height = 1; // Making Height Equal One
		}

		// Reset The Current Viewport
		gl.glViewport((int) this.initX, (int) this.initY, width, height);

		gl.glMatrixMode(GL10.GL_PROJECTION); // Select The Projection Matrix
		gl.glLoadIdentity(); // Reset The Projection Matrix

		// Calculate The Aspect Ratio Of The Window
		GLU.gluPerspective(gl, 90, (float) width / (float) height, 0, 0.0f);

		/* this function put the coordinates in the top left of the screen */
		GLU.gluOrtho2D(gl, 0, width, height, 0);

		gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
		gl.glLoadIdentity(); // Reset The Modelview Matrix
	}
}